//
//  XLBindClientAlertView.m
//  industry
//
//  Created by xl on 2020/12/24.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLBindClientAlertView.h"

@interface XLBindClientAlertView ()

@property (nonatomic, strong) XLScrollView *bgView;
@property (nonatomic, strong) XLLabel *titleLB;
@property (nonatomic, strong) XLView *textBgView;
@property (nonatomic, strong) XLTextField *contentTF;
@property (nonatomic, strong) XLButton *cancelBtn;
@property (nonatomic, strong) XLButton *bindBtn;

@end
@implementation XLBindClientAlertView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12.0f;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)showAlertView {
    [self hideAllView];
    
    self.frame = CGRectMake(0, 0, 250, 164);
    self.centerX = SCREEN_WIDTH/2.0;
    self.centerY = SCREEN_HEIGHT/2.0;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.bgView];
    [_bgView addSubview:self];
    
    [self addSubview:self.titleLB];
    [self addSubview:self.textBgView];
    [_textBgView addSubview:self.contentTF];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.bindBtn];
    
    [self makeConstraints];
}

- (void)makeConstraints {
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(20);
        make.centerX.equalTo(self);
    }];
    
    [_textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.titleLB.mas_bottom).offset(20);
         make.centerX.equalTo(self);
         make.size.mas_equalTo(CGSizeMake(200, 36));
    }];
    
    [_contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textBgView).offset(10);
        make.right.equalTo(self.textBgView).offset(-10);
        make.top.bottom.equalTo(self.textBgView);
    }];
    
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textBgView);
        make.top.equalTo(self.textBgView.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(94, 30));
    }];
    
    [_bindBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.textBgView);
        make.top.equalTo(self.textBgView.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(94, 30));
    }];
}


- (void)hideAllView {
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_bgView removeFromSuperview];
    _titleLB = nil;
    _bindBtn = nil;
    _cancelBtn = nil;
    _textBgView = nil;
    _contentTF = nil;
    _bgView = nil;
}

- (NSString *)fetchTextFieldContent {
    return _contentTF.text;
}

#pragma mark - UIButton
- (void)clickCancel {
    [self endEditing:YES];
    [self hideAllView];
}


- (void)clickBind {
    [self endEditing:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickBindWithContent:)]) {
        [self.delegate clickBindWithContent:_contentTF.text];
    }
}

#pragma mark - TextField
- (void)textFieldChange:(XLTextField *)sender {
    if (sender.text.length > 0) {
        [self changeBindBtnStatus:YES];
    } else {
        [self changeBindBtnStatus:NO];
    }
}

#pragma mark - Private
- (void)changeBindBtnStatus:(BOOL)status {
    if (status) {
        _bindBtn.userInteractionEnabled = YES;
        _bindBtn.alpha = 1;
    } else {
        _bindBtn.userInteractionEnabled = NO;
        _bindBtn.alpha = 0.3;
    }
}

#pragma mark - Setter/getter
- (XLScrollView *)bgView {
    if (!_bgView) {
        _bgView = [XLScrollView new];
        _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _bgView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.3];
        _bgView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT + 1);
    }
    return _bgView;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:14];
        _titleLB.textColor = [UIColor blackFontColor];
        _titleLB.text = @"输入邀请码绑定客户";
    }
    return _titleLB;
}

- (XLView *)textBgView {
    if (!_textBgView) {
        _textBgView = [XLView new];
        _textBgView.backgroundColor = [UIColor whiteColor];
        _textBgView.layer.cornerRadius = 6;
        _textBgView.layer.borderWidth = 1.0;
        _textBgView.layer.borderColor = [UIColor grayFourBgColor].CGColor;
        _textBgView.layer.masksToBounds = YES;
    }
    return _textBgView;
}

- (XLTextField *)contentTF {
    if (!_contentTF) {
        _contentTF = [XLTextField new];
        _contentTF.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        _contentTF.textColor = [UIColor blackFontColor];
        [_contentTF addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _contentTF;
}

- (XLButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [XLButton new];
        _cancelBtn.backgroundColor = [UIColor colorWithHex:0xEBEBEB];
        _cancelBtn.layer.cornerRadius = 15.0f;
        _cancelBtn.layer.masksToBounds = YES;
        _cancelBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        [_cancelBtn setTitleColor:[UIColor grayFontColor] forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (XLButton *)bindBtn {
    if (!_bindBtn) {
        _bindBtn = [XLButton new];
        _bindBtn.backgroundColor = [UIColor blueBgColor];
        _bindBtn.layer.cornerRadius = 15.0f;
        _bindBtn.layer.masksToBounds = YES;
        _bindBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        [_bindBtn setTitleColor:[UIColor whiteFontColor] forState:UIControlStateNormal];
        [_bindBtn setTitle:@"绑定客户" forState:UIControlStateNormal];
        [_bindBtn addTarget:self action:@selector(clickBind) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bindBtn;
}

@end
